<template>
  <el-dialog :title="action === 'add' ? '新增' : '修改'" width="30%" draggable 
                :close-on-click-modal="false" v-model="visible" @open="submitted = false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmit(dataFormRef)"
      label-width="80px">
      <el-form-item label="角色代码" prop="code">
        <el-input v-model="dataForm.code" minlength="2" maxlength="50" placeholder="角色代码" />
      </el-form-item>
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="dataForm.name" minlength="2" maxlength="20" placeholder="角色名称" />
      </el-form-item>
      <el-form-item label="备注" prop="description">
        <el-input v-model="dataForm.description" maxlength="100" placeholder="备注" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="ad-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit(dataFormRef)" :disabled="submitted">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import http from "@/utils/httpRequest";
import validate from "@/utils/validate";
import type { FormInstance } from "element-plus";
import { ref, reactive, nextTick, defineExpose, defineEmits } from "vue";

// 操作：add-新增，update-修改
const action = ref('');

// 弹窗是否显示
const visible = ref(false);

// 提交状态
const submitted = ref(false);
// 表单数据
const dataForm = reactive({
  id: undefined,
  code: '',
  name: '',
  description: ''
});

// 数据校验规则
const dataRule = reactive({
  code: validate.required('资源代码'),
  name: validate.required('资源名称')
});

// 表单引用
const dataFormRef = ref<FormInstance>();

// 父级组件的自定义事件
const emit = defineEmits(['loadRoleTableData']);

// 表单初始化
const initForm = async (a, data) => {
  action.value = a;
  // 新增操作
  if (a === 'add') {
    visible.value = true;
    await nextTick();
    dataFormRef.value.resetFields();
    dataForm.id = undefined;
    dataForm.code = '';
    dataForm.name = '';
    dataForm.description = '';
    return;
  }
  // 修改操作
  http
    .post({
      url: "/role/info",
      params: { 'roleId': data.id },
    })
    .then(async ({ data }) => {
      if (http.success(data)) {
        visible.value = true;
        await nextTick();
        dataFormRef.value.resetFields();
        if (data.body) {
          dataForm.id = data.body.id;
          dataForm.code = data.body.code;
          dataForm.name = data.body.name;
          dataForm.description = data.body.description;
        }
      }
    });
};

// 表单提交
const dataFormSubmit = (form: FormInstance | undefined) => {
  if (submitted.value) {
    return;
  }
  form.validate((valid) => {
    if (valid) {
      submitted.value = true;
      http
        .post({
          url: "/role/save",
          data: dataForm,
        })
        .then(async ({ data }) => {
          if (http.success(data)) {
            visible.value = false;
            await nextTick();
            emit("loadRoleTableData");
            return;
          }
          submitted.value = false;
        });
    }
  })
};

// 暴露方法
defineExpose({ initForm });
</script>
